package com.nstc.settings.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.nstc.settings.model.FileResult;


public class FileUtil {
    
    private static final String AppService = "AppService";
    private static final String SmartPage = "SmartPage";
    private static final String CFB_Web = "CFB-Web";
    private static final String CIP = "CIP";
    private static final String CIPF = "CIPF";
    private static final String EBD_Web = "EBD-Web";
    private static final String PMS = "PMS";
    private static final String portal = "portal";
    private static final String UAS = "UAS";

	/**
	 * @param args
	 */
	public static Map<String,List<FileResult>> readFile(String path) throws IOException {
		
		List<FileResult> list = new ArrayList<FileResult>();
		list = getAllProperties(path,list);
		Map<String,List<FileResult>> map = new TreeMap<String,List<FileResult>>();
		for (int i = 0; i < list.size(); i++) {
			FileResult f = list.get(i);
			String key = "";
			if(f.getFilePath().contains("-CFG")) {
				for (String string : f.getFilePath().split("\\\\")) {
					if(string.contains("-CFG")) {
						String name = string.replaceAll("-CFG", "");
						key = name;
					}
				}
			} else if (f.getFilePath().contains(AppService)){
				key = AppService;
			} else if (f.getFilePath().contains(SmartPage)) {
				key = SmartPage;
			} else if (f.getFilePath().contains(CFB_Web)) {
                key = CFB_Web;
    		} else if (f.getFilePath().contains(CIP)) {
                key = CIP;
        	} else if (f.getFilePath().contains(CIPF)) {
                key = CIPF;
            } else if (f.getFilePath().contains(EBD_Web)) {
                key = EBD_Web;
            } else if (f.getFilePath().contains(PMS)) {
                key = PMS;
            } else if (f.getFilePath().contains(portal)) {
                key = portal;
            } else if (f.getFilePath().contains(UAS)) {
                key = UAS;
            }
			if ("".equals(key)) {
			    continue;
			}
			List<FileResult> fileList = map.get(key);
			if (fileList == null) {
				fileList = new ArrayList<FileResult>();
				fileList.add(f);
				map.put(key, fileList);
			} else {
			    fileList.add(f);
			}
		}
		return map;


	}
	 public static List<FileResult> getAllProperties(String path, List<FileResult> list){
		  File rootDir = new File(path);
		   if(!rootDir.isDirectory()){
			   if (rootDir.isFile() && rootDir.getName().indexOf("properties") > -1) {
				   FileResult f = new FileResult();
				   f.setFileName(rootDir.getName());
				   f.setFilePath(rootDir.getPath());
				   list.add(f);
			   }
		   }else{
		    String[] fileList =  rootDir.list();
		    for (int i = 0; i < fileList.length; i++) {
		     path = rootDir.getAbsolutePath()+"\\"+fileList[i];
		     getAllProperties(path, list);     
		      } 
		   }    
		  return list;    
		 }

}
